home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / copyfile.icl < prev    next >
Encoding:
Text File  |  1996-02-23  |  2.5 KB  |  86 lines  |  [TEXT/3PRM]

  1. module copyfile
  2.  
  3. /*
  4. Commandline version of a file copying program.
  5.  
  6. Run the program using the "Basic Values Only" option.
  7.  
  8. */
  9.  
  10. import StdEnv, StdFile
  11.  
  12. Start::*World -> *File
  13. Start world =  fwrites "\nGoodbye.\n" stdinout`
  14. where 
  15.     (_,stdinout`)    = CommandLoop files1 stdinout
  16.     (stdinout ,files1)    = stdio files
  17.     (files ,_)    = openfiles world
  18.         
  19.  
  20. CommandLoop::Files *File -> (Files,*File)
  21. CommandLoop files stdio =  CommandLoop` files` stdio`
  22. where 
  23.     (files`,stdio`)    = Copy files stdio
  24.         
  25.  
  26. CommandLoop`::Files *File -> (Files,*File)
  27. CommandLoop` files stdio
  28.     | answer<>'y' && answer<>'Y'    =  (files,stdio2)
  29.                                     =  CommandLoop` files` stdio`
  30. where 
  31.     (files`,stdio`)    = Copy files stdio2
  32.     answer             = FirstChar answ
  33.     (answ  ,stdio2)    = freadline stdio1
  34.     stdio1             = fwrites "\nCopy another file (y/n)? " stdio
  35.         
  36.  
  37. Copy::Files *File -> (Files,*File)
  38. Copy files io
  39.     | source == dest    =  (files, fwrites "\nCopying succeeded.\n" io4)
  40.                         =  CopyFile (StripNewline source) (StripNewline dest) files io4
  41. where 
  42.     (dest,io4)        = freadline io3
  43.     io3                = fwrites "\nDestination file: " io2
  44.     (source,io2)    = freadline io1
  45.     io1                = fwrites "\nSource file: " io
  46.  
  47. CopyFile::String String Files *File -> (Files,*File)
  48. CopyFile source dest files io
  49.     |    not sopen    =     (files1,alert1)
  50.     |    not dopen    =     (files2,alert2)
  51.     |    io_error    =     (files4,alert3)
  52.     |    not dclose    =     (files4,alert4)
  53.     |    not sclose    =     (files4,alert5)
  54.                     =     (files4,alert6)
  55. where 
  56.     (sclose,files4)             = fclose sfile` files3
  57.     (dclose,files3)             = fclose dfile` files2
  58.     (io_error,sfile`,dfile`)    = CopyFiles sfile dfile
  59.     (dopen,dfile,files2)        = fopen dest FWriteText files1
  60.     (sopen,sfile,files1)        = fopen source FReadData files
  61.  
  62.     alert1    = fwrites "\nCopying failed.\nSource file could not be opened.\n" io
  63.     alert2    = fwrites "Copying failed.\nDestination file could not be opened.\n"  io
  64.     alert3    = fwrites "Copying failed.\nFile I/O error.\n" io
  65.     alert4    = fwrites "Copying failed.\nDestination file could not be closed.\n"  io
  66.     alert5    = fwrites "Copying failed.\nSource file could not be closed.\n"  io
  67.     alert6    = fwrites "\nCopying succeeded.\n" io
  68.         
  69.  
  70. CopyFiles::*File *File -> (Bool, *File, *File)
  71. CopyFiles source dest
  72.     | srcend || wrterror    =  (wrterror,source1,dest1)
  73.                             =  CopyFiles source2 (fwritec byte dest1)
  74. where 
  75.     (_,byte,source2)    = freadc source1
  76.     (srcend,source1)        = fend source
  77.     (wrterror,dest1)        = ferror dest
  78.  
  79. StripNewline::String -> String
  80. StripNewline "" =  ""
  81. StripNewline str =  str % (0, size str - 2)
  82.  
  83. FirstChar::String -> Char
  84. FirstChar "" =  ' '
  85. FirstChar str =  str.[0]
  86.